package cn.com.git.admin.api;


import cn.com.git.admin.config.security.LoginContext;
import cn.com.git.admin.service.UpmsService;
import cn.com.git.admin.service.dto.SysUserDetailDTO;
import cn.com.git.admin.service.dto.SysUserListDTO;
import cn.com.git.admin.service.dto.SysUserSaveDTO;
import cn.com.git.admin.service.query.SysUserListQuery;
import cn.com.git.common.base.BaseApi;
import cn.com.git.common.model.ResultConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 用户管理
 * @author zhangzhenfu
 * @Description
 * @date 2020/5/18 15:32
 */
@Api(value = "用户管理", description = "用户管理")
@RestController
@RequestMapping("/sys_user")
public class SysUserApi extends BaseApi {

    @Autowired
    private LoginContext loginContext;

    @Autowired
    private UpmsService upmsService;

    @ApiOperation(value = "列表查询", notes = "列表查询", httpMethod = "POST",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/list/page",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultPage<SysUserListDTO> listPage(@Valid @RequestBody SysUserListQuery query) {
        ResultPage<SysUserListDTO> resultPage ;
        try {
            resultPage = upmsService.listSysUser(query);
        }catch (Throwable e){
            resultPage = new ResultPage<>(ResultConstant.FAILED);
            logger.error("列表查询失败：{}",e.getMessage());
        }
        return resultPage ;
    }

    @GetMapping(value = "/find",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询", httpMethod = "GET", notes = "根据id查询数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", paramType = "query", dataType = "integer"),
    })
    public ResultMsg<SysUserDetailDTO> find(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg<SysUserDetailDTO> resultMsg ;
        try {
            resultMsg = upmsService.findSysUser(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("查询失败：{}",e.getMessage());
        }
        return resultMsg ;
    }


    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<SysUserSaveDTO> add(@Valid @RequestBody SysUserSaveDTO form) {
        ResultMsg<SysUserSaveDTO> resultMsg = null ;
        try {
            resultMsg = upmsService.addSysUser(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("新增失败：{}",e.getMessage());
        }
        return resultMsg ;
    }
    @ApiOperation(value = "编辑", notes = "编辑", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/edit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<SysUserSaveDTO> edit(@Valid @RequestBody SysUserSaveDTO form) {
        ResultMsg<SysUserSaveDTO> resultMsg ;
        try {
            resultMsg = upmsService.editSysUser(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("编辑失败：{}",e.getMessage());
        }
        return resultMsg ;
    }

    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除", httpMethod = "GET", notes = "根据id删除数据",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", paramType = "query", dataType = "integer"),
    })
    public ResultMsg delete(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg resultMsg ;
        try {
            resultMsg = upmsService.deleteSysUser(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("删除失败：{}",e.getMessage());
        }
        return resultMsg ;
    }


}
